<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { toNumber } from 'lodash-es'
import useClipboard from 'vue-clipboard3'
import { GetData, PatchModifyType } from '@/api/modules/givefacialmaskact'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()

const findFrom = ref({
  Title: '',
  ActStatus: '',
})
const dataList: any = reactive({
  list: [],
  option: [],
})
const options = ref([
  { value: 0, label: '未开始' },
  { value: 1, label: '开始' },
  { value: -1, label: '结束' },
])
const loading = ref(true)
const total = ref(0)
let PageSize = 20
let PageNum = toNumber(localStorage.getItem('currentPage')) || 1

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.Title && ({ Title: findFrom.value.Title })),
    ...(findFrom.value.ActStatus !== '' && ({ ActStatus: findFrom.value.ActStatus })),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      dataList.list.map((item: any) => {
        // item.ticketsArray = item.ticketsArray.slice(1, -1)
        // item.ticketsArray = item.ticketsArray.split(',')
        item.ticketsArray = JSON.parse(item.ticketsArray)
        return item
      })
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 修改状态
let switchlock = true
function ModifyType(params: any) {
  if (switchlock) {
    switchlock = false
    PatchModifyType(params).then((res: any) => {
      if (res.statusCode === 200) {
        ElMessage.success('修改成功')
        // GetDataList(PageNum, PageSize)
        switchlock = true
      }
      else {
        ElMessage.error(`修改失败${res.errors}`)
      }
    })
  }
}
// 添加
function AddData() {
  router.push({
    name: 'givefacialmaskactDetail',
  })
}
// 编辑
function EditData(id: number) {
  localStorage.setItem('currentPage', `${PageNum}`)
  router.push({
    name: 'givefacialmaskactDetail',
    query: {
      id,
    },
  })
}

// 复制
async function CopyUrl(id: number) {
  const clipboard = useClipboard()
  try {
    // 使用 navigator.clipboard.writeText 来复制文本
    // await navigator.clipboard.writeText(`pages/adPage/adPage?landing_id=${id}&pushCoupons=0`)
    await clipboard.toClipboard(`/pages/sendFacePack/sendFacePack?MaskId=${id}`)
    ElMessage.success('复制成功')
  }
  catch (error) {
    const textarea = document.createElement('textarea')
    textarea.value = `/pages/sendFacePack/sendFacePack?MaskId=${id}`
    document.body.appendChild(textarea)
    textarea.select()
    // 使用现代浏览器支持的复制命令
    const successful = document.execCommand('copy')
    if (!successful) {
      ElMessage.error('复制失败')
    }
    else {
      ElMessage.success('复制成功')
    }
    document.body.removeChild(textarea)
  }
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return toNumber(localStorage.getItem('currentPage')) || 1
})

onMounted(() => {
  if (localStorage.getItem('currentPage') !== null) {
    GetDataList(toNumber(localStorage.getItem('currentPage')), 20)
    localStorage.removeItem('currentPage')
  }
  else {
    GetDataList()
  }
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="活动主题">
                <el-input v-model="findFrom.Title" placeholder="请输入活动主题" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="活动状态">
                <el-select v-model="findFrom.ActStatus" placeholder="请选择" clearable>
                  <el-option label="未开始" :value="0" />
                  <el-option label="开始" :value="1" />
                  <el-option label="结束" :value="-1" />
                </el-select>
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column header-align="center" prop="title" label="活动主题" width="300" />
        <el-table-column header-align="center" prop="shareTxt" label="分享语" width="300" />
        <el-table-column align="center" prop="shareImg" label="分享图" width="100">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.shareImg" />
          </template>
        </el-table-column>
        <el-table-column header-align="center" prop="descriptionTxt" label="试用说明文案" width="300" />
        <el-table-column align="center" prop="descriptionPic" label="试用说明图片" width="100">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.descriptionPic" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="bgColor" label="背景色" width="110">
          <template #default="scope">
            <el-color-picker v-model="scope.row.bgColor" disabled />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="processBgColor" label="进度条背景色" width="110">
          <template #default="scope">
            <el-color-picker v-model="scope.row.processBgColor" disabled />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="processColor" label="进度条色" width="110">
          <template #default="scope">
            <el-color-picker v-model="scope.row.processColor" disabled />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="ticketBaoBg" label="卡包弹窗背景图" width="100">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.ticketBaoBg" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="背景图" width="150">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.bgImg" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="卡包图标" width="150">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.ticketBaoIcon" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="getTicketBtn" label="领取按钮" width="380">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.getTicketBtn" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="shareBtn" label="分享按钮" width="380">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.shareBtn" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="noFinishBtn" label="未达成按钮" width="280">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.noFinishBtn" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="finishBtn" label="已领取按钮" width="280">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.finishBtn" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="getListIcon" label="用户已领取按钮" width="280">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.getListIcon" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="nogetListIcon" label="用户未领取按钮" width="280">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.nogetListIcon" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="bottomLogo" label="底部logo" width="380">
          <template #default="scope">
            <el-image style="width: auto; height: 60px;" :src="imgDomain + scope.row.bottomLogo" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="buyVipPic" label="背景图" width="500">
          <template #default="scope">
            <el-scrollbar>
              <div class="scrollbar-flex-content">
                <el-image v-for="(item, index) in scope.row.ticketsArray" :key="index" class="scrollbar-flex-img" :src="imgDomain + item" />
              </div>
            </el-scrollbar>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" fixed="right" width="200">
          <template #default="scope">
            <el-segmented
              v-model="scope.row.actStatus"
              :options="options"
              @change="ModifyType({ id: scope.row.id, actStatus: scope.row.actStatus })"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" min-width="230">
          <template #default="scope">
            <el-button type="primary" @click="EditData(scope.row.id)">
              编辑
            </el-button>
            <el-button type="info" @click="CopyUrl(scope.row.id)">
              复制路径
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 60px;
    margin-right: 10px;
  }
}
</style>
